﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.Networking;
public class Castle : NetworkBehaviour
{
    //variables visible in the inspector
    [SyncVar(hook = "ChangHp")]
    public float lives = 200;
    public GameObject fracture;

    void Update()
    {
        //destroy castle when lives are 0
        if (lives <= 0f)
        {
            lives = 0;

            //if a fractured version of this part of the castle has been assigned, instantiate it in order to have a cool destruction effect (not recommended on mobile devices)
            if (fracture && gameObject.name != "Castle gate" && gameObject.name != "1")
            {
                GameObject temp = Instantiate(fracture, transform.position, Quaternion.Euler(0, transform.eulerAngles.y, 0));
                //NetworkServer.Spawn(temp);
            }
            else if (fracture)
            {
                GameObject temp = Instantiate(fracture, transform.position, Quaternion.Euler(0, 0, 0));
                //NetworkServer.Spawn(temp);
            }

            Destroy(gameObject);
        }
    }
    public void BeAttacked(int att)
    {
        lives -= att;
        if (!this.transform.parent.transform.GetComponent<Camp>().chargeServer)
            return;
        Debug.Log("attack");
        lives -= att;
    }
    public void ChangHp(float newHP)
    {
        lives = newHP;
    }

}
